package com.platform.modules.wallet.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.dao.WalletDailyBillDao;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.domain.WalletAccount;
import com.platform.modules.wallet.domain.WalletDailyBill;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.service.WalletDailyBillhService;
import com.platform.modules.wallet.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("walletDailyBillhService")
public class WalletDailyBillServiceImpl extends BaseServiceImpl<WalletDailyBill> implements WalletDailyBillhService {

    @Resource
    private WalletAccountService walletAccountService;
    @Resource
    private WalletDailyBillDao walletDailyBillDao;
    @Resource
    private ChatUserService chatUserService;
    @Resource
    private WalletService walletService;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(walletDailyBillDao);
    }


    @Override
    public void createDailyBill() {

        ChatUser chatUser = new ChatUser();
        List<ChatUser> chatUsers = chatUserService.queryList(chatUser);

        for (ChatUser user : chatUsers) {
            List<WalletAccount> queryYesterday = walletAccountService.queryYesterday(user.getUserId());
            int yesterday = queryYesterday.stream().mapToInt(WalletAccount::getAmount).sum();
            if (yesterday != 0) {
                List<WalletAccount> queryToday = walletAccountService.queryToday(user.getUserId());
                int today = queryToday.stream().mapToInt(WalletAccount::getAmount).sum();
                Wallet wallet = walletService.findById(user.getUserId());
                if (!hasTodyBill(user.getUserId())){
                    this.create(wallet.getUserId(), yesterday, wallet.getAmount() - today);
                }
            }
        }
    }

    @Override
    public WalletDailyBill getLastOne(Long userId) {
        return new WalletDailyBill().selectOne(
                new LambdaQueryWrapper<WalletDailyBill>()
                        .eq(WalletDailyBill::getUserId,userId)
                        .orderByDesc(WalletDailyBill::getId)
                        .last("limit 1"));
    }

    @Override
    public List<WalletDailyBill> queryById(Long userId) {
        return new WalletDailyBill().selectList(
                new LambdaQueryWrapper<WalletDailyBill>()
                        .eq(WalletDailyBill::getUserId,userId));
    }

    private boolean hasTodyBill(Long userId){
        Date date = DateUtil.date();
        List<WalletDailyBill> walletDailyBills = new WalletDailyBill().selectList(
                new LambdaQueryWrapper<WalletDailyBill>()
                        .eq(WalletDailyBill::getUserId, userId)
                        .eq(WalletDailyBill::getCreateDate, date));
        if (walletDailyBills.size() > 0){
            return true;
        }
        return false;
    }

    private void create(Long userId, Integer amount, Integer balance) {
        DateTime date = DateUtil.date();
        WalletDailyBill walletDailyBill = new WalletDailyBill()
                .setUserId(userId)
                .setAmount(amount)
                .setBalance(balance)
                .setCreateTime(date)
                .setCreateDate(date);
        try {
            this.add(walletDailyBill);
        }catch (Exception e){
            log.error(e.toString());
        }
    }
}
